package manager.auth.controller;
import common.CookieUtils;
import common.ResponseEntity;
import io.swagger.annotations.Api;
import manager.auth.service.UserAuthService;
import manager.auth.utils.ResponseUtils;
import manager.auth.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@RestController
@RequestMapping(value = {"/admin/auth"})
@Api(value = "授权中心")
public class AuthAdminController {
    @Autowired
    private UserAuthService userAuthService;
    //登录
    @PostMapping("/login")
    public ResponseEntity<UserInfo> login(HttpServletResponse httpServletResponse, @RequestBody UserInfo userInfo) {
            String token = userAuthService.authentication(userInfo.getUsername(), userInfo.getPassword());
            return ResponseUtils.getResponse(token,httpServletResponse,"shop_admin_tick_tocken");
    }

    //校验token是否有效
    @GetMapping("/verify")
    @RequestMapping(value = "verify", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<UserInfo> verify(HttpServletRequest request, HttpServletResponse response) {
            return ResponseUtils.virifyCookie(request,"shop_admin_tick_tocken");
    }
    //退出登录
    @GetMapping("/unaccredit")
    @RequestMapping(value = "unaccredit", method = {RequestMethod.POST,RequestMethod.GET})
    public ResponseEntity<String> accredit(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        CookieUtils.setCookie(request, response, "shop_admin_tick_tocken",
                null, null, null, true);
        ResponseEntity<String> responseEntity = new ResponseEntity();
        responseEntity.setStatusCode(200);
        return responseEntity;
    }
}
